package cn.dckeji.dcssm.controller;


import cn.dckeji.dcssm.common.model.ResultModel;
import cn.dckeji.dcssm.entity.Demo;
import cn.dckeji.dcssm.service.DemoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 演示类 前端控制器
 * </p>
 *
 * @author Kenny
 * @since 2019-02-26
 */
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoService demoService;

    /**
     * 分页查询用户列表
     * @param pageNumber
     * @param pageSize
     * @param userName
     * @return
     */
    @GetMapping("/page")
    public JSONObject page(Integer pageNumber, Integer pageSize,
                                  @RequestParam(value = "userName", required = false)String userName) {
        QueryWrapper<Demo> demoQueryWrapper = new QueryWrapper<>();
        demoQueryWrapper.orderByDesc("gmt_create");
        if (StringUtils.isNotBlank(userName)) {
            demoQueryWrapper.like("user_name", userName);
        }
        IPage<Demo> demoIPage = demoService.page(new Page<>(pageNumber, pageSize), demoQueryWrapper);
        return ResultModel.page(demoIPage.getTotal(), demoIPage.getRecords());
    }

    /**
     * 保存
     * @param demo
     * @return
     */
    @PostMapping("/saveDemo")
    public ResultModel<Object> saveDemo(Demo demo) {
        if (StringUtils.isNotBlank(demo.getId())) {
            demo.setGmtModified(LocalDateTime.now());
            demoService.updateById(demo);
        }else {
            demo.setGmtCreate(LocalDateTime.now());
            demoService.save(demo);
        }
        return ResultModel.success();
    }

    /**
     * 测试接收参数
     * 需要注意此接收方式，在前端传入 ajax 传入参数时需要加
     * 属性声明参数格式必须为json：contentType:"application/json"
     * 传入参数必须使用JSON.stringify(subData)转换后传入
     * @param jsonString
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新")
    ResultModel<Object> update(@RequestBody String jsonString) {
        System.out.println(jsonString);
        Map map = JSONObject.parseObject(jsonString, Map.class);
        Object id = map.get("id");
        Object userName = map.get("userName");
        Object userAge = map.get("userAge");
        Object userSex = map.get("userSex");
        Object userContent = map.get("userContent");
        System.out.println(id);
        System.out.println(userName);
        System.out.println(userAge);
        System.out.println(userSex);
        System.out.println(userContent);
        Demo demo = new Demo();
        demo.setUserAge(Integer.parseInt(userAge+""));
        demo.setUserContent(String.valueOf(userContent));
        demo.setUserName(String.valueOf(userName));
        demo.setUserSex(Integer.parseInt(userSex+""));
        demo.setGmtModified(LocalDateTime.now());
        if (id != null && StringUtils.isNotBlank(id.toString())) {
            demo.setId(String.valueOf(id));
            demo.setGmtModified(LocalDateTime.now());
            demoService.updateById(demo);
        }else {
            demo.setGmtCreate(LocalDateTime.now());
            demoService.save(demo);
        }
//        Demo demo = new Demo();
//        demo.setGmtModified(LocalDateTime.now());
//        demoService.updateById(demo);
        return ResultModel.success();
    }
    /**
     * 删除
     * @param demoId
     * @return
     */
    @PostMapping("/deleteById")
    public ResultModel<Object> deleteById(String demoId) {
        demoService.removeById(demoId);
        return ResultModel.success();
    }
}

